package com.qf.fmall2203.user.controller;

import com.qf.fmall2203.user.entity.Users;
import com.qf.fmall2203.user.service.IUsersService;
import com.qf.fmall2203.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户  前端控制器
 * </p>
 *
 * @author dongjl
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin
public class UsersController {

    @Autowired
    IUsersService usersService;

    @RequestMapping("/regist")
    public ResultVo register(@RequestBody Users users){
        log.debug("users收参为:",users);

        ResultVo map = usersService.regist(users);

        return  map;
    }

    @GetMapping("/login")
    public ResultVo login(String username ,String password){
        log.debug("username为: username,password : password");
        Subject subject = SecurityUtils.getSubject();
        //设置免登录
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);

        subject.login(usernamePasswordToken);

        Users principal = (Users) subject.getPrincipal();
        return ResultVo.ok(principal);
    }


}
